package com.etone.autotesting.test;

import java.io.File;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.jar.JarEntry;
import java.util.jar.JarInputStream;

import org.junit.Test;

import com.etone.autotesting.listener.WebServiceClassAnnotationListener;
import com.etone.autotesting.util.ClassUtil.ClassFileFilter;
import com.impetus.annovention.Discoverer;
import com.impetus.annovention.Filter;
import com.impetus.annovention.FilterImpl;

public class ClassUtilTestCase {

	// @Test
	public void testClassUtil() throws Exception {
		File rootFile = new File(this.getClass().getResource("/").getFile().replaceFirst("/", ""));
		if (!rootFile.getPath().endsWith("lib")) {
			String lobFile = rootFile.getParent() + "\\" + "lib";
			rootFile = new File(lobFile);
			System.out.println(rootFile);
		}
		getFiles(rootFile);
	}

	// ;

	private Map getFiles(File libFile) throws Exception {
		Map fileMap = new HashMap<String, Class>();
		File[] fileList = libFile.listFiles(new ClassFileFilter());
		if (fileList != null && fileList.length > 0) {
			String path = "file:" + fileList[0].getAbsolutePath();
			InputStream inputStream = new URL(path).openStream();
			JarInputStream stream = new JarInputStream(inputStream);
			JarEntry jarEntity;
			while ((jarEntity = stream.getNextJarEntry()) != null) {
				String classFileName = jarEntity.getName();
				if (jarEntity.getMethod() == 8 && classFileName.contains("Service")) {
					int classIndex = classFileName.indexOf(".class");
					String className = classFileName.substring(0, classIndex).replace("/", ".");
					Class clazz = Class.forName(className);
					if (clazz.isInterface())
						System.out.println(jarEntity.getName() + "," + jarEntity.getMethod());
				}
			}
			System.out.println(stream);
			// stream.
			// setSonList(se, libFile.getPath() + "\\", fileMap);
		}
		return fileMap;
	}

	public static Class<?> setSonList(File rootFile, String parentDirectory, Map fileMap) {
		if (rootFile.isDirectory()) {
			File[] files = rootFile.listFiles();
			for (File file : files) {
				Class<?> clazz = setSonList(file, parentDirectory, fileMap);
				if (clazz != null && fileMap.containsKey(clazz.getSimpleName())) {
					fileMap.put(clazz.getSimpleName(), clazz);
				}
			}
		} else {
			String className = null;
			try {
				if (rootFile.getPath().indexOf(".class") != -1) {
					className = rootFile.getPath().replace(parentDirectory, "").replace(".class", "").replace("\\", ".");
					Class<?> classObject = Class.forName(className);
					if (classObject.isInterface() && classObject.getSimpleName().endsWith("Service")) {
						return classObject;
					}
				}
			} catch (ClassNotFoundException e) {
				System.err.println("找不到类 " + className);
			} catch (ClassCastException e) {
				System.err.println(className);
			}
		}
		return null;
	}

	

	@Test
	public void testClassLoaderUtil() {
		Discoverer discoverer = new Discoverer() {

			@Override
			public Filter getFilter() {
				return new FilterImpl();
			}

			@Override
			public URL[] findResources() {
				List<URL> urlList = new ArrayList<URL>();
				File rootFile = new File(this.getClass().getResource("/").getFile().replaceFirst("/", ""));
				if (!rootFile.getPath().endsWith("lib")) {
					String lobFile = rootFile.getParent() + "\\" + "lib";
					rootFile = new File(lobFile);
					System.out.println(rootFile);
				}
				String path = "file:" + rootFile.getAbsolutePath() + "\\ssmp-service-3.0.0-SNAPSHOT.jar";
				try {
					urlList.add(new URL(path));
				} catch (MalformedURLException e) {
					e.printStackTrace();
				}
				return urlList.toArray(new URL[urlList.size()]);
			}
		};
		// Register class annotation listener
		WebServiceClassAnnotationListener classlistener = new WebServiceClassAnnotationListener();
		discoverer.addAnnotationListener(classlistener);
		// Fire it
		discoverer.discover();

		Map<String, Class> classMap = classlistener.getClassMap();
		Iterator<Map.Entry<String, Class>> classIterator = classMap.entrySet().iterator();
		while (classIterator.hasNext()) {
			Map.Entry<String, Class> mapClass = classIterator.next();
			System.out.println(mapClass.getKey() + " | " + mapClass.getValue());
		}
	}
}
